#!/usr/bin/ruby

require 'test/unit'
require 'lib/rire'

class TestGrep < Test::Unit::TestCase
  def setup
    @dsl = Rire::DSL.new($rire_home + '/plugin')
    @dsl.instance_eval <<-EOD
      def execute(src)
        instance_eval(src, 'unit.dsl', 1)
      end
    EOD
  end

  def test_no_key
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 14 },
          { :key => 'aa', :val => 15 },
          { :key => 'cc', :val => 12 },
        ]

        filter.grep
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('dd', ctx.entries[1].key)
    assert_equal('bb', ctx.entries[2].key)
    assert_equal('aa', ctx.entries[3].key)
    assert_equal('cc', ctx.entries[4].key)
  end

  def test_key
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ae', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.grep :config => {
          :key => 'key', :regexp => 'a[a-z]'
        }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('ae', ctx.entries[1].key)
    assert_equal(2, ctx.entries.length)
  end

  def test_no_regexp
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ee', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.grep :config => {
          :key => 'key',
        }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('dd', ctx.entries[1].key)
    assert_equal('bb', ctx.entries[2].key)
    assert_equal('ee', ctx.entries[3].key)
    assert_equal('cc', ctx.entries[4].key)
    assert_equal(5, ctx.entries.length)
  end

  def test_grep_invert_false
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ae', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.grep :config => {
          :key => 'key', :regexp => 'a[a-z]', :invert => false
        }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('aa', ctx.entries[0].key)
    assert_equal('ae', ctx.entries[1].key)
    assert_equal(2, ctx.entries.length)
  end

  def test_grep_invert_true
    src = <<-SCENARIO
      define_task do
        subscribe.const :config => [
          { :key => 'aa', :val => 10 },
          { :key => 'dd', :val => 12 },
          { :key => 'bb', :val => 15 },
          { :key => 'ae', :val => 14 },
          { :key => 'cc', :val => 11 },
        ]

        filter.grep :config => {
          :key => 'key', :regexp => 'a[a-z]', :invert => true
        }
      end
    SCENARIO

    ctx = @dsl.execute(src)

    assert_equal('dd', ctx.entries[0].key)
    assert_equal('bb', ctx.entries[1].key)
    assert_equal('cc', ctx.entries[2].key)
    assert_equal(3, ctx.entries.length)
  end

  def teardown
  end
end

